package com.tyl.servlet.provider;

import com.alibaba.fastjson.JSONArray;
import com.tyl.pojo.Provider;
import com.tyl.pojo.User;
import com.tyl.service.provider.ProviderService;
import com.tyl.service.provider.ProviderServiceImpl;
import com.tyl.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ProviderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String method =req.getParameter("method");
        if (method.equals("query")&&method!=null){
            this.query(req,resp);
        }else if(method.equals("view")&&method!=null){
            this.getProviderById(req,resp, "providerview.jsp");
        }else if(method.equals("add")&&method!=null){
            this.addProvider(req,resp);
        }else if(method.equals("delprovider")&&method!=null){
            this.deleteProvider(req,resp);
        }else  if(method.equals("modify")&&method!=null){
            this.getProviderById(req,resp, "providermodify.jsp");
        }else if(method.equals("modifyYes")&&method!=null){
            this.modifysave(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }


    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String queryProCode=req.getParameter("queryProCode");
        String queryProName=req.getParameter("queryProName");
        if (queryProCode==null){
            queryProCode="";
        }
        if (queryProName==null){
            queryProName="";
        }
        List<Provider> list = new ArrayList<Provider>();
        ProviderService providerService=new ProviderServiceImpl();
        list= providerService.getProviderList(queryProCode, queryProName);
        req.setAttribute("providerList",list);
        req.setAttribute("queryProCode",queryProCode);
        req.setAttribute("queryProName",queryProName);
        try {
            req.getRequestDispatcher("providerlist.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void getProviderById(HttpServletRequest req, HttpServletResponse resp,String url) throws ServletException, IOException {
        String id = req.getParameter("proid");
        if (id!=null) {
            ProviderService providerService = new ProviderServiceImpl();
            Provider provider = null;
            try {
                provider = providerService.getProviderById(id);
                req.setAttribute("provider", provider);
                req.getRequestDispatcher(url).forward(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void addProvider(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //从网页上获取供应商新信息
        String proCode =req.getParameter("proCode");
        String proName =req.getParameter("proName");
        String proContact =req.getParameter("proContact");
        String proPhone =req.getParameter("proPhone");
        String proAddress =req.getParameter("proAddress");
        String proFax =req.getParameter("proFax");
        String proDesc =req.getParameter("proDesc");

        // 把网页上的供应商信息放到对象中
        Provider provider=new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProAddress(proAddress);
        provider.setProFax(proFax);
        provider.setProDesc(proDesc);

        provider.setCreatedBy(((User) req.getSession().getAttribute(Constants.USER_SESSION)).getId());
        provider.setCreationDate(new Date());

        System.out.println(provider);

        ProviderService providerService=new ProviderServiceImpl();
        int flag= providerService.addProvider(provider);
        if (flag>0){
            resp.sendRedirect(req.getContextPath()+"/jsp/provider.do?method=query");
        }else{
            req.getRequestDispatcher("provideradd.jsp").forward(req, resp);
        }
    }


    public void deleteProvider(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pid=req.getParameter("proid");
        int flag=0;
        HashMap<String, String> map = new HashMap<String, String>();
        if (pid!=null){
            ProviderService providerService=new ProviderServiceImpl();
            flag = providerService.deleteProvider(pid);
            if (flag==0){//删除成功
                map.put("delResult","true");
            }else if (flag==-1){//删除失败
                map.put("delResult","false");
            }else if (flag>0){
                map.put("delResult", String.valueOf(flag));
            }
        }else{//订单不存在
            map.put("delResult","notexist");
        }
        //把resultMap转换成json对象输出
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        writer.write(JSONArray.toJSONString(map));
        writer.flush();
        writer.close();
    }

    public void modifysave(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        String proCode =req.getParameter("proCode");
        String proName =req.getParameter("proName");
        String proContact =req.getParameter("proContact");
        String proPhone =req.getParameter("proPhone");
        String proAddress =req.getParameter("proAddress");
        String proFax =req.getParameter("proFax");
        String proDesc=req.getParameter("proDesc");
        String proId=req.getParameter("uid");

        Provider provider=new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProAddress(proAddress);
        provider.setProFax(proFax);
        provider.setProDesc(proDesc);
        provider.setModifyDate(new Date());
        provider.setModifyBy(((User) req.getSession().getAttribute(Constants.USER_SESSION)).getId());
        provider.setId(Integer.parseInt(proId));
        int flag=0;
        ProviderService providerService=new ProviderServiceImpl();
        flag = providerService.updateProvider(provider);
        if (flag>0){//修改成功
            resp.sendRedirect(req.getContextPath()+"/jsp/provider.do?method=query");
        }else{
            req.getRequestDispatcher("providermodify.jsp").forward(req,resp);
        }
    }

}
